<?php

$PATH = 'bonsai/common';
require_once "$PATH/file/File.php";
require_once "$PATH/file/Image.php";
require_once "$PATH/util/Collection.php";

$PATH = 'cpanel';
require_once "$PATH/model/entity/Language.php";
require_once "$PATH/model/dao/LanguageDAO.php";
require_once "$PATH/business/LanguageBusiness.php";
require_once "$PATH/controller/CPanelController.php";

/**
 * Enter description here ...
 * @author NgoAnhTu
 * @date 2013-10-13
 */
final class LanguageController extends CPanelController {
	
	const VIEW_PATH = 'cpanel/view/lang/';
	const NAME = 'lang';
	
	/**
	 * @var LanguageBusiness
	 */
	private $languageService;
	
	/**
	 * Constructor
	 */
	public function LanguageController () {
		parent::CPanelController();
		$this->languageService = LanguageBusiness::getInstance();
	}
	
	/**
	 * (non-PHPdoc)
	 * @see SecurityController::checkSecurity()
	 */
	protected function checkSecurity() {
// 		if (!isset($_SESSION[Dispatcher::USER_SESSION])) {
// 			$this->redirect('user', 'login');
// 		}
	}

	/**
	 * Action: View all Languages
	 * @action(grid)
	 */
	public function grid($codes) {
		$MSG_CONFIRM = str_replace(LG::ENTITY, LG::LANG, LG::MSG_DEL_CONFIRM);
		if (!HTTP::isGet()) {
			$this->languageService->active($codes);
			$this->msg(self::NAME, 'grid', '', str_replace(LG::ENTITY, LG::LANG, LG::MSG_UPDATE_STATUS), 2);
		} 
		$languages = $this->languageService->findAll();
		require_once self::VIEW_PATH . 'grid.html';
	}
	
	/**
	 * Action: Create new Langauge
	 * @action(create)
	 */
	public function create($code, $name, $img) {
		$msg = '';
		
		if (!HTTP::isGet()) {
			$code = strtolower(trim($code));
			$name = trim($name);
			
			if (!$this->languageService->isExist($code, $name)) {
				$lang = $this->languageService->create($code, $name, $img);
				if ($lang != null) {
					$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
					$this->msg(self::NAME, 'grid', '', $msg);
				} else {
					$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_NOT_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				}
			} else {
				$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_EXIST);
				$msg = str_replace(LG::VAL, "$code : $name", $msg);
			}
		}
		
		require_once self::VIEW_PATH . 'create.html';
	}

	/**
	 * Action: Edit Language
	 * @action(edit) 
	 */
	public function edit($code, $name, $img) {
		$code = strtolower(trim($code));
		$name = trim($name);
		$msg = '';
		
		$lang = $this->languageService->findByCode($code);
		if ($lang == null) {
			$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_NOT_EXIST);
			$msg = str_replace(LG::VAL, $code, $msg);
			$this->msg(self::NAME, 'grid', '', $msg);
		}
		
		if (!HTTP::isGet()) {
			if (!$this->languageService->isExistOther($code, $name)) {
				$lang->name = $name;
				$lang = $this->languageService->update($lang, $img);
				if ($lang != null) {
					$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				} else {
					$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_NOT_SAVE);
					$msg = str_replace(LG::VAL, $name, $msg);
				}
				$this->msg(self::NAME, 'grid', '', $msg);
			} else {
				$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_EXIST);
				$msg = str_replace(LG::VAL, "$code : $name", $msg);
			}
		}
		
		require_once self::VIEW_PATH . 'edit.html';
	}
	
	/**
	 * Action: Delete Language
	 * @action(del) 
	 */
	public function del($code) {
		$msg = '';
	
		if ($this->languageService->findByCode($code) == null) {
			$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_NOT_EXIST);
			$msg = str_replace(LG::VAL, $code, $msg);
		} else {
			if ($this->languageService->del($code) != null) {
				$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_DELETED);
				$msg = str_replace(LG::VAL, $code, $msg);
			} else {
				$msg = str_replace(LG::ENTITY, LG::LANG, LG::MSG_NOT_DEL);
				$msg = str_replace(LG::VAL, $code, $msg);
			}
		}
		
		$this->msg(self::NAME, 'grid', '', $msg);
	}
	
}
?>